import { Button, Tooltip } from 'antd';
import { Fragment } from 'react';
import { EditOutlined, EyeOutlined, ReconciliationOutlined, DownloadOutlined } from '@ant-design/icons';
import { VersionManageAPI } from '../common/type';
import { tooltipLength, } from '@/utils';
import pageStyles from '@/common/pageLayout.less';
import MyIcon from '@/components/MyIcon';
/** 版本管理页面功能操作 */
const getManageFunctions = (
  /** 资源权限参数 */
  curRes: Record<string, any>,
  /** 数据操作参数 */
  operateFnc: VersionManageAPI.IVersionTableActionType,
  /** 数据操作对象 */
  record: Record<string, any>,
) => {
  /** 功能操作集 */
  let operationList: any = [];

  /** 编辑操作 */
  const EditFunc = record?.id &&
    curRes?.['/versionController/edit'] &&
    (record?.state == 0 || record?.state == 2) && (
      <Fragment key={record?.id + 'EditOutlined'}>
        <Button
          className='manageBtntTxtStyle'
          type={'link'}
          size={'small'}
          icon={<EditOutlined />}
          onClick={() => {
            operateFnc?.onEdit(record);
          }}
        >
          编辑
        </Button>
      </Fragment>
    );

  /**
   * 查看详情
   */
  const View = curRes['/versionController/getVersionByID/{id}'] && (
    <Fragment key={record?.id + 'EyeOutlined'}>
      <Button
        className='manageBtntTxtStyle'
        onClick={() => {
          operateFnc?.onView(record);
        }}
        type={'link'}
        size={'small'}
        icon={<EyeOutlined />}
      >
        查看
      </Button>
    </Fragment>
  );

  /**提交审核 */
  // const Apply = curRes['/versionController/apply'] &&
  //   record?.createrID == getUserID() &&
  //   (record?.state == 0 || record?.state == 2) && (
  //     <Fragment key={record?.id + 'AuditOutlined'}>
  //       <Popconfirm
  //         title={<span style={{ fontWeight: 600 }}>确认提交审核吗？</span>}
  //         onConfirm={(e) => {
  //           e?.stopPropagation();
  //           operateFnc?.onApply(record);
  //         }}
  //         // showCancel={false}
  //         okType={'primary'}
  //       >
  //         <Button
  //           className='manageBtntTxtStyle'
  //           type={'link'}
  //           size={'small'}
  //           icon={<AuditOutlined />}
  //         >
  //           提交
  //         </Button>
  //       </Popconfirm>
  //     </Fragment>
  //   );

  /**作废 */
  // const Cancel = curRes?.['/versionController/checkIssue'] &&
  //   // record?.checkerID == getUserID() &&
  //   record?.state == 1 && (
  //     <Fragment key={record?.id + 'FileExcelOutlined'}>
  //       <Button
  //         className='manageBtntTxtStyle'
  //         onClick={() => {
  //           operateFnc?.onCancel(record);
  //         }}
  //         type={'link'}
  //         size={'small'}
  //         icon={<FileExcelOutlined />}
  //       >
  //         作废
  //       </Button>
  //     </Fragment>
  //   );

  /**办理审核 */
  const Check = curRes?.['/versionController/checkIssue'] &&
    record?.state == 1 && (
      <Fragment key={record?.id + 'ReconciliationOutlined'}>
        <Button
          className='manageBtntTxtStyle'
          onClick={() => {
            operateFnc?.goToCheck(record);
          }}
          type={'link'}
          size={'small'}
          icon={<ReconciliationOutlined />}
        >
          审核
        </Button>
      </Fragment>
    );

  /**办理审核 */
  const Download = record?.versionType != '2' && (
    <Fragment key={record?.id + 'DownloadOutlined'}>
      <Button
        className='manageBtntTxtStyle'
        onClick={() => {
          operateFnc?.onDownload(record);
        }}
        type={'link'}
        size={'small'}
        icon={<DownloadOutlined />}
      >
        下载
      </Button>
    </Fragment>
  );

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button
        style={{ color: '#000' }}
        className='manageBtntTxtStyle'
        type={'link'}
        size={'small'}
        disabled
      >
        --
      </Button>
    </Fragment>
  );

  operationList = [View, EditFunc, Check, Download];
  operationList = operationList?.filter((itemBtn: any) => itemBtn);
  return operationList && operationList?.length > 0 ? operationList : [Disable];
};

/**
 * 版本管理数据表格信息 展示和操作
 * @returns
 */
const getVersionManageTabColumns = (
  /** 资源权限参数 */
  curRes: Record<string, any>,
  /** 数据操作参数 */
  operateFnc: VersionManageAPI.IVersionTableActionType,
  /** 根据头部key参数 展示不用的数据 */
  visibleFlag: Record<string, any>,
) => {
  return [
    {
      title: '版本类型',
      dataIndex: 'versionType',
      key: 'versionType',
      width: 130,
      visible: true,
      unDrag: true,
      fixed: 'left',
      sorter: (a: any, b: any) => (a.versionType > b.versionType ? 1 : -1),
      render: (text: string | null, record: Record<string, any>) => {
        let newText: string = '';
        let arr = [];
        arr = record?.versionUrl?.split('/');
        // let attachList = {
        //   attachName: arr[arr.length - 1],
        //   attachUrl: record?.versionUrl,
        // };
        if (text == '0') {
          newText = '安卓端';
        } else if (text == '1') {
          newText = '苹果端';
        } else if (text == '2') {
          newText = '电脑端';
        }
        return (
          <Tooltip>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {newText ? newText : '--'}
              &nbsp;&nbsp;
              {text == '0' ? (
                <MyIcon type={'icon-android'} />
              ) : text == '1' ? (
                <MyIcon type={'icon-apple'} />
              ) : text == '2' ? (
                <MyIcon type={'icon-windows'} />
              ) : null}
              {/* &nbsp;&nbsp;
              {record?.versionType != '2' && (
                <a
                  onClick={() => {
                    message.success('下载中，请稍后...');
                    downloadTemplate(
                      '/versionController/downloadFile',
                      attachList?.attachName,
                      attachList?.attachUrl,
                      () => {},
                    );
                  }}
                >
                  <MyIcon type={'icon-download'} />
                </a>
              )} */}
            </span>
          </Tooltip>
        );
      },
    },
    // {
    //   title: '版本号',
    //   dataIndex: 'publicNum',
    //   key: 'publicNum',
    //   width: 80,
    //   visible: false,
    //   render: (text: string | null, record: Record<string, any>) => {
    //     return (
    //       <Tooltip title={tooltipLength(text, 100)}>
    //         <span
    //           className={pageStyles['over-flow-hidden']}
    //           style={{ maxWidth: 240 }}
    //         >
    //           {text ? Number(text)?.toFixed(1) : '--'}
    //         </span>
    //       </Tooltip>
    //     );
    //   },
    //   sorter: (a: any, b: any) => {
    //     if (a.VersionNumber < b.VersionNumber) {
    //       return -1;
    //     } else {
    //       return 1;
    //     }
    //   },
    // },
    {
      title: '升级内容',
      dataIndex: 'upgradeNote',
      key: 'upgradeNote',
      width: 200,
      visible: true,
      render: (_text: string | null, record: Record<string, any>) => {
        let newText: string = '';
        newText = record?.upgradeNote?.contentText;
        return (
          <Tooltip title={tooltipLength(newText, 100)}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {newText ? newText : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '版本状态',
      dataIndex: 'isPublic',
      key: 'isPublic',
      width: 120,
      visible: visibleFlag?.isPublic,
      // visible: true,
      sorter: (a: any, b: any) => (a.isPublic > b.isPublic ? 1 : -1),
      render: (text: string | null) => {
        let newText: string = '';
        let textColor = '';
        if (text == '0') {
          newText = '未发布';
          textColor = '#00FFFF';
        } else if (text == '1') {
          newText = '发布上线';
          textColor = '#22ff00';
        } else if (text == '2') {
          newText = '历史版本';
          textColor = '#868686';
        }
        return (
          <span
            className={pageStyles['over-flow-hidden']}
            style={{ color: textColor }}
          >
            {/* <MyIcon type={'icon-dian'} style={{ color: textColor }} /> */}
            {newText ? newText : '--'}
          </span>
        );
      },
    },
    {
      title: '审核状态',
      dataIndex: 'state',
      key: 'state',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => (a.state > b.state ? 1 : -1),
      render: (text: string | null) => {
        let newText: string = '';
        let textColor = '';
        if (text == '0') {
          newText = '起草';
          textColor = '#ff008c';
        } else if (text == '1') {
          newText = '待审核';
          textColor = '#cc00ff';
        } else if (text == '2') {
          newText = '未通过';
          textColor = '#ff0000';
        } else if (text == '3') {
          newText = '已通过';
          textColor = '#22ff00';
        } else if (text == '4') {
          newText = '已作废';
          textColor = '#940909';
        }
        return (
          <span
            className={pageStyles['over-flow-hidden']}
            style={{ color: textColor }}
          >
            {newText ? newText : '--'}
          </span>
        );
      },
    },
    {
      title: '发布时间',
      dataIndex: 'signTime',
      key: 'signTime',
      width: 150,
      visible: visibleFlag?.signTime,
      // visible: true,
      sorter: (a: any, b: any) => (a.signTime > b.signTime ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '发布人',
      dataIndex: 'signerName',
      key: 'signerName',
      width: 110,
      visible: true,
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '发布岗位',
      dataIndex: 'signPositionName',
      key: 'signPositionName',
      width: 120,
      visible: false,
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '发布部门',
      dataIndex: 'signDepartmentName',
      key: 'signDepartmentName',
      width: 120,
      visible: false,
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '发布公司',
      dataIndex: 'signCompanyName',
      key: 'signCompanyName',
      width: 120,
      visible: false,
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      width: 150,
      visible: false,
      sorter: (a: any, b: any) => (a.createTime > b.createTime ? 1 : -1),
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '创建人',
      dataIndex: 'createrName',
      key: 'createrName',
      width: 110,
      visible: false,
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '创建岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      width: 120,
      visible: false,
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '创建部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      width: 120,
      visible: false,
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '创建公司',
      dataIndex: 'createCompanyName',
      key: 'createCompanyName',
      width: 120,
      visible: false,
      render: (text: string | null) => {
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {text ? text : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请时间',
      dataIndex: 'applyTime',
      key: 'applyTime',
      width: 150,
      visible: visibleFlag?.applyTime,
      // visible: true,
      sorter: (a: any, b: any) => (a.applyTime > b.applyTime ? 1 : -1),
      render: (text: string | null, record: Record<string, any>) => {
        let newText: string = '';
        newText = record?.msysVersionCheck?.applyTime;
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {newText ? newText : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请人',
      dataIndex: 'applyerName',
      key: 'applyerName',
      width: 110,
      visible: visibleFlag?.applyerName,
      // visible: true,
      render: (text: string | null, record: Record<string, any>) => {
        let newText: string = '';
        newText = record?.msysVersionCheck?.applyerName;
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {newText ? newText : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请岗位',
      dataIndex: 'applyPositionName',
      key: 'applyPositionName',
      width: 120,
      visible: visibleFlag?.applyPositionName,
      // visible: false,
      render: (text: string | null, record: Record<string, any>) => {
        let newText: string = '';
        newText = record?.msysVersionCheck?.applyPositionName;
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {newText ? newText : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请部门',
      dataIndex: 'applyDepartmentName',
      key: 'applyDepartmentName',
      width: 120,
      visible: visibleFlag?.applyDepartmentName,
      // visible: false,
      render: (text: string | null, record: Record<string, any>) => {
        let newText: string = '';
        newText = record?.msysVersionCheck?.applyDepartmentName;
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {newText ? newText : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '申请公司',
      dataIndex: 'applyCompanyName',
      key: 'applyCompanyName',
      width: 120,
      visible: visibleFlag?.applyCompanyName,
      // visible: false,
      render: (text: string | null, record: Record<string, any>) => {
        let newText: string = '';
        newText = record?.msysVersionCheck?.applyCompanyName;
        return (
          <Tooltip title={text}>
            <span
              className={pageStyles['over-flow-hidden']}
            >
              {newText ? newText : '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 220,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (_text: any, record: any) => {
        return (
          <div
            style={{
              display: 'flex',
            }}
          >
            {getManageFunctions(curRes, operateFnc, record)}
          </div>
        );
      },
    },
  ];
};

export { getVersionManageTabColumns };
